#ifndef COOPERATIVE_MODE_H
#define COOPERATIVE_MODE_H

#include <cstdio> // debug
#include <iostream> // debug

class CooperativeMode : public cnc::base::CooperativeTask
{
protected:
	CooperativeMode &operator=(CooperativeMode &); // copying disabled
	const cnc::base::OperationModeID thisMode_;
	cnc::base::KernelSetup *coordinator_;
	cnc::ui::Services *uisrv_;
	cnc::ui::VKServices *vksrv_;
public:
	CooperativeMode(cnc::base::OperationModeID thisMode) : thisMode_(thisMode)
	{
		cnc::base::CNCKernel *kernel = cnc::base::getCNCKernel();
		coordinator_ = kernel->getKernelSetup();
		cnc::ui::Manager *mgr = cnc::ui::getManager();
		uisrv_ = mgr->getServices();
		cnc::ui::VirtualKeyboard *vk = cnc::ui::getVirtualKeyboard();
		vksrv_ = vk->getVKServices();
//		cnc::compiler::ProgramManager *pm = cnc::compiler::getProgramManager();
//		cnc::ui::setProgramManager(pm);
	}
	bool selectMode(cnc::base::OperationModeID nextMode)
	{
		if (nextMode != thisMode_) {
			if (nextMode != cnc::base::OPMODE_END)
				coordinator_->selectMode(nextMode);
			return false;
		}
		return true;
	}
	bool createDebugConsoleWindow()
	{
		if (!AllocConsole())
	        return false;
		// Reopen file handles for stdin, stdout and sterr to point to
		// the newly created console
		freopen("CONIN$",  "r", stdin);
		freopen("CONOUT$", "w", stdout);
		freopen("CONOUT$", "w", stderr);
		std::cout << "DEBUG console has been created!" << std::endl;
	    return true;
	}
};

#endif
